package com.project.android;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.widget.Toast;

public class DBAdaptateur {
	private static final int BASE_V=1;
	private static final String BASE_NOM="Alphabet.db";
	private SQLiteDatabase MaBasedonnees;
    private MabaseOpenHelper baseHelper;
	Context contx;
	public DBAdaptateur(Context contx) {
	baseHelper=new MabaseOpenHelper(contx, BASE_NOM, null, BASE_V);
	this.contx=contx;
	}
	public SQLiteDatabase open(){
		MaBasedonnees=baseHelper.getWritableDatabase();
		Truncate();
		requeteInsert();
		return MaBasedonnees;
	}
	public void close(){
	MaBasedonnees.close();
	}
	public void Truncate(){
	MaBasedonnees.execSQL("DELETE FROM lettres");
	}
	public SQLiteDatabase getBaseDonnees(){
		return MaBasedonnees;
	}
	
	public long insererlettre(int im,int s,String le){
	ContentValues values = new ContentValues();
	try{
		values.put("image", im);
		values.put("son", s);
		values.put("lettre", le);
		return MaBasedonnees.insert("lettres", null, values);
	} catch (Exception e) {
        Toast.makeText(contx, "Echec d'Ajout", Toast.LENGTH_LONG).show();
        return MaBasedonnees.insert("lettres", null, values);
    }
		}
    public void requeteInsert(){
	int tabIM[]={0x7f020001,0x7f020003,0x7f020004,0x7f020005,0x7f020006,0x7f020007};
	int tabSON[]={0x7f040000,0x7f040001,0x7f040002,0,0,0,0,0,0,0,0,0,0};
	long a;
	for(int i=0;i<tabIM.length;i++)
	a=insererlettre(tabIM[i],tabSON[i],"A");
    }

	public boolean supprimerlettre(int id){
		return MaBasedonnees.delete("lettres", "_id="+id, null)>0;
	}
	
	public Cursor recupererLettre(){
	String select="SELECT * FROM lettres";
	Cursor c = MaBasedonnees.rawQuery(select, null);
	return c;
	}
	
	
}
